package com.shangcheng.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shangcheng.entity.TCustomer;
import com.shangcheng.entity.TOrders;
import com.shangcheng.entity.TShopCart;
import com.shangcheng.entity.TShopCartView;
import com.shangcheng.mapper.TCustomerMapper;
import com.shangcheng.mapper.TShopCarMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShopCartServiceImpl {
    @Autowired
    private TShopCarMapper dao;
    @Autowired
    private TCustomerMapper customerDao;
    //清空购物车
    @Transactional
    public void clearShopByCustmerId(Integer customerId) {
        dao.delete(new QueryWrapper<TShopCart>().eq("custmer_id", customerId));
    }
    //添加购物车
    @Transactional
    public void addShopCart(TShopCart shopCart) {
        dao.insert(shopCart);
    }
    //查询购物车列表
    @Transactional(readOnly = true)
    public List<TShopCartView> findShopCartViews(Integer custmerId){
        return dao.findShopCartViews(custmerId);
    }
    //删除购物车商品
    @Transactional
    public void deleteShopCart(int orderId) {
        dao.delete(new QueryWrapper<TShopCart>().eq("ID", orderId));
    }
    //提交完订单删除购物车
    @Transactional
    public void deleteShopCartByCus(int customerId) {
        QueryWrapper<TShopCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("custmer_id", customerId);
        dao.delete(queryWrapper);
    }
    @Transactional
    public String getAddressById(int customerId){
        QueryWrapper<TCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", customerId);
        TCustomer customer = customerDao.selectOne(queryWrapper);
        return customer.getAddress();
    }
}
